﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DroidLord.app.dy.data
{

    class DYPopularGoodList
    {
        //返回是否正确
        public bool isResponseOk = false;
        //解析是否正确
        public bool isParseOk = false;
        public List<DYPopularGoodItem> itemList = new List<DYPopularGoodItem>();


        private static string getValue(JObject myobj, string key)
        {
            try
            {
                return myobj[key].ToString();
            }
            catch (Exception eee)
            {

            }
            return "";
        }
        private static DYPopularGoodItem parseItem(string itemJson)
        {
            DYPopularGoodItem itemObj = new DYPopularGoodItem();
            var myobj = JObject.Parse(itemJson);


            itemObj.videoId = getValue(myobj, "item_id");
            string authorstring = getValue(myobj, "author");
            if (!authorstring.Equals(""))
            {
                var authorObj = JObject.Parse(authorstring);
                itemObj.userId = getValue(authorObj, "id");
                itemObj.userName = getValue(authorObj, "name");
                itemObj.avatar = getValue(authorObj, "avatar");

            }
            string goodstring = getValue(myobj, "goods");
            if (!goodstring.Equals(""))
            {
                var goodObj = JObject.Parse(goodstring);
                itemObj.productId = getValue(goodObj, "product_id");
                itemObj.title = getValue(goodObj, "title");
                itemObj.price = getValue(goodObj, "price");
                itemObj.marketPrice = getValue(goodObj, "market_price");
                itemObj.score = getValue(goodObj, "score");
                itemObj.sales = getValue(goodObj, "sales");
                itemObj.goodImage = getValue(goodObj, "cover");
                if (!itemObj.goodImage.Equals(""))
                {
                    itemObj.goodImage = "http://p1.pstatp.com/aweme/720x720/" + itemObj.goodImage;
                }
            }


            return itemObj;
        }

        public static DYPopularGoodList parse(string popluarInfo)
        {
            DYPopularGoodList itemList = new DYPopularGoodList();
            var myobj = JObject.Parse(popluarInfo);
            string code = getValue(myobj, "code");
            if (code.Equals("0"))
            {
                itemList.isResponseOk = true;
                itemList.isParseOk = true;
                string datastring = getValue(myobj, "data");
                var dataObj = JObject.Parse(datastring);
                string rankliststring = dataObj["rank_list"].ToString();
                JArray myarray = JArray.Parse(rankliststring);
                int len = myarray.Count;
                for (int i = 0; i < len; i++)
                {
                    string itemstring = myarray[i].ToString();
                    DYPopularGoodItem itemDy = parseItem(itemstring);
                    itemList.itemList.Add(itemDy);
                }

            }
            else
            {
                itemList.isResponseOk = false;
            }
            return itemList;
        }

    }
    class DYPopularGoodItem
    {
        
        //item_id
        public string videoId = "";
        public string userId = "";

        public string userName = "";
        //头像
        public string avatar = "";
        //商品ID
        public string productId = "";
        //
        public string title = "";
        //销售价
        public string price = "";
        //市场价，原价
        public string marketPrice = "";
        //人气值
        public string score = "";
        //商品图片
        //http://p1.pstatp.com/aweme/720x720/cmp-ecom-alliance/Fjl_g9b6HOde5wR7GXTUe-vBVqzq.jpg
        public string goodImage = "";
        //销量
        public string sales = "";

        
    }
}
